<template>
  <layout>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      >
        <a-button class="action-button" type="primary" @click="search()"
          >搜索</a-button
        >
        <a-button class="action-button" type="primary" @click="tableAdd"
          >新增</a-button
        >
      </custom-form>
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
    </section>

    <!-- 产品列表 -->
    <a-table
      :columns="tableColumns"
      :dataSource="tableData"
      :rowKey="(record) => record.power_id"
      :customRow="customTableRow"
      :pagination="false"
      bordered
    >
      <template slot="is_menu" slot-scope="row">
        {{ getLabel(row.is_menu, [], 'is_menu') }}
      </template>
      <!-- 按钮区 -->
      <template slot="action" slot-scope="row">
        <a-button
          class="action-button"
          type="link"
          @click="handleTableEdit(row)"
          >编辑</a-button
        >
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button class="action-button" type="link">删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <!-- <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination> -->

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      @cancel="visible = false"
      @ok="handleSubmit"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        v-bind="formItemLayout"
        layout="horizontal"
      ></custom-form>
      <!-- <section slot="footer"> -->
      <!-- <a-button
        @click="handleSubmit"
        type="primary"
      >提交</a-button> -->
      <!-- </section> -->
    </a-modal>
  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { poserServices, IPower, IPowerGroup } from '@/common/services/power'
import { MenuTypeEnum } from '@/common/enum'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
  },
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: Object,
  })
  public currentPower!: IPower

  // 产品列表搜索条件
  public tableSearch = {
    pow_gp_nam: '',
  }

  // 公共参数
  public commonParams = {
    power_up_id: '',
    is_menu: '2',
  }

  // 打开抽屉
  public drawerVisible = false

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '按钮编号',
      dataIndex: 'power_id',
      key: 'power_id',
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        disabled: true,
        isShowList: false,
        isShowFrom: false,
      },
    },
    // {
    //   title: '按钮名称',
    //   dataIndex: 'power_up_id',
    //   key: 'power_up_id',
    //   form: {
    //     type: 'a-tree-select',
    //     placeholder: '',
    //     isShowList: false,
    //     treeData: [],
    //     treeDefaultExpandAll: true,
    //     replaceFields: {
    //       title: 'power_nam',
    //       key: 'power_id',
    //       value: 'power_id'
    //     }
    //   }
    // },
    {
      title: '按钮名称',
      dataIndex: 'power_nam',
      key: 'power_nam',
      form: {
        type: 'a-input',
        placeholder: '请输入按钮名称',
        isShowQuery: true,
      },
    },
    {
      title: '按钮key',
      dataIndex: 'key',
      key: 'key',
      form: {
        type: 'a-input',
        placeholder: '请输入key',
      },
    },
    {
      title: '接口路径',
      dataIndex: 'power_url',
      key: 'power_url',
      form: {
        type: 'a-input',
        placeholder: '请输入接口路径',
      },
    },
    {
      title: '操作',
      key: 'action',
      width: 120,
      scopedSlots: { customRender: 'action' },
    },
  ]

  public tableApi = {
    // @ts-ignore
    list: (data) =>
      poserServices.getPowerNextList(
        {
          power_id: data.power_up_id,
        },
        [MenuTypeEnum.BUTTON]
      ),
    add: poserServices.updatePower,
    edit: poserServices.updatePower,
    del: poserServices.deletePower,
  }

  get power_id() {
    return this.currentPower.power_id
  }

  public created() {
    console.log(this.currentPower.power_id, 1)
  }

  public mounted() {
    console.log(this.currentPower.power_id, 2)
  }

  @Watch('currentPower.power_id', { immediate: true })
  public handlerChangePid() {
    this.commonParams.power_up_id = this.currentPower.power_id
    this.commonParams.power_up_id && this.getData()
  }

  public async getData() {
    const res = await this.getTableList()
    this.tableData = res.data || []
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: IPower) {
    this.tableEdit(Object.assign({}, row))
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
